﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using STF.OrangeDog.IService;
using STF.OrangeDog.IRepository;
using STF.OrangeDog.Dtos;
using STF.OrangeDog.Model;

namespace STF.OrangeDog.Service
{
    public class UsersInfoBll
    {
        private IUsersInfoDal _db;

        public UsersInfoBll(IUsersInfoDal db)
        {
            _db = db;
        }
        public async Task<int> AddUsersInfoAsync(string RealName, DateTime Birthday, int Gender, string WeChat, string Tel, string Address, string Photo, decimal Amount, int Uid)
        {
            return await _db.AddModelAsync(new UsersInfo
            {
                RealName = RealName,
                Birthday = Birthday,
                Gender = Gender,
                WeChat = WeChat,
                Tel = Tel,
                Address = Address,
                Photo = Photo,
                Amount = Amount,
                uid = Uid
            });
        }

        public async Task<int> DeleteUsersInfoAsync(int Id)
        {
            var data = await _db.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            if (data.DeleteId == 1)
                return 0;

            return await _db.RemoveModelAsync(data);
        }

        public async Task<int> EditUsersInfoAsync(int Id, string RealName, DateTime Birthday, int Gender, string WeChat, string Tel, string Address, string Photo, decimal Amount, int Uid)
        {
            var data = await _db.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            data.RealName = RealName;
            data.Birthday = Birthday;
            data.Gender = Gender;
            data.WeChat = WeChat;
            data.Tel = Tel;
            data.Address = Address;
            data.Photo = Photo;
            data.Amount = Amount;
            data.uid = Uid;
            data.UpdateTime = DateTime.Now;
            return await _db.EditModelAsync(data);
        }

        public async Task<IEnumerable<UsresInofDtos>> GetAll(int DeleteId = 1)
        {
            var data = await _db.GetDataByChooseAsync(r => r.DeleteId == DeleteId);
            return data != null ? data.Select(r => new UsresInofDtos
            {
                id = r.Id,
                RealName = r.RealName,
                Birthday = r.Birthday,
                Gender = r.Gender,
                WeChat = r.WeChat,
                Tel = r.Tel,
                Address = r.Address,
                Photo = r.Photo,
                Amount = r.Amount,
                uid = r.uid,
                
            }) : new List<UsresInofDtos>();
        }

        public async Task<UsresInofDtos> GetDataById(int Id)
        {
            var data = await _db.GetDataByIdAsync(Id);
            return data == null ? new UsresInofDtos() : new UsresInofDtos
            {
                id = data.Id,
                RealName = data.RealName,
                Birthday = data.Birthday,
                Gender = data.Gender,
                WeChat = data.WeChat,
                Tel = data.Tel,
                Address = data.Address,
                Photo = data.Photo,
                Amount = data.Amount,
                uid = data.uid,
               
            };
        }
    }
}
